﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IPC.Communication.Framework.Core;
using IPC.Communication.Framework.Core.Types;
using IPC.Communication.Framework.Profinet.Melsec.Helper;
using IPC.Communication.Framework.Reflection;
using IPC.Communication.Framework.Serial;

namespace IPC.Communication.Framework.Profinet.Melsec
{
    public class MelsecA3CNet : SerialDeviceBase, IReadWriteA3C, IReadWriteDevice, IReadWriteNet
    {
        private byte station = 0;
        private bool _sumCheck = true;
        private int _format = 1;

        public byte Station
        {
            get
            {
                return station;
            }
            set
            {
                station = value;
            }
        }

        public bool SumCheck
        {
            get { return _sumCheck; }
            set { _sumCheck = value; }
        }


        public int Format
        {
            get { return _format; }
            set { _format = value; }
        }


        public MelsecA3CNet()
        {
            base.ByteTransform = new RegularByteTransform();
            base.WordLength = 1;
        }

        [HslMqttApi("BatchRead", "批量读取")]
        public OperateResult<Dictionary<string, object>> BatchRead(Dictionary<string, DataTypeEnum> addresses, int batchNumber)
        {
            return new OperateResult<Dictionary<string, object>>(StringResources.Language.NotSupportedFunction);
        }

        [HslMqttApi("BatchWrite", "批量写入")]
        public OperateResult BatchWrite(Dictionary<string, object> addresses, int batchNumber)
        {
            return new OperateResult(StringResources.Language.NotSupportedFunction);
        }

        [HslMqttApi("ReadByteArray", "")]
        public override OperateResult<byte[]> Read(string address, ushort length)
        {
            return MelsecA3CNetHelper.Read(this, address, length);
        }

        [HslMqttApi("WriteByteArray", "")]
        public override OperateResult Write(string address, byte[] value)
        {
            return MelsecA3CNetHelper.Write(this, address, value);
        }

        [HslMqttApi("ReadBoolArray", "")]
        public override OperateResult<bool[]> ReadBool(string address, ushort length)
        {
            return MelsecA3CNetHelper.ReadBool(this, address, length);
        }

        [HslMqttApi("WriteBoolArray", "")]
        public override OperateResult Write(string address, bool[] value)
        {
            return MelsecA3CNetHelper.Write(this, address, value);
        }

        [HslMqttApi]
        public OperateResult RemoteRun()
        {
            return MelsecA3CNetHelper.RemoteRun(this);
        }

        [HslMqttApi]
        public OperateResult RemoteStop()
        {
            return MelsecA3CNetHelper.RemoteStop(this);
        }

        [HslMqttApi]
        public OperateResult<string> ReadPlcType()
        {
            return MelsecA3CNetHelper.ReadPlcType(this);
        }

        public override string ToString()
        {
            return String.Format("MelsecA3CNet[{0}:{1}]", base.PortName, base.BaudRate);
        }
    }
}
